package com.ibtest.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ibatis.common.util.PaginatedArrayList;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.mapping.statement.PaginatedDataList;
import com.ibtest.vo.Car;

public class CarsServiceImpl implements ICarsService {
	Logger log = Logger.getLogger(this.getClass());

	private SqlMapClient sqlMapClient = null;

	public SqlMapClient getSqlMapClient() {
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMap) {
		this.sqlMapClient = sqlMap;
	}
	
	@SuppressWarnings({"deprication"})
	public List getAllCars() {
		try {
			
			return sqlMapClient.queryForList("cars.getAllCars", null);//sqlMapClient.queryForList("cars.getAllCars");
			
		}catch(SQLException sqlEx) {
			log.error("Exception while getting Cars data:" + sqlEx.getLocalizedMessage());
		}
		
		return null;
	}
	
	public Car getCar() {
		try {
			return (Car)sqlMapClient.queryForObject("cars.getCar", "India");
		}catch(SQLException sqlEx) {
			log.error("Exception while getting Car data:" + sqlEx.getLocalizedMessage());
		}
		return null;
	}
	
	public int addCar() {
		try {
			sqlMapClient.insert("cars.addCar", (new Car("abc", "Australia")));
		} catch (SQLException e) {
			log.error("Exception while adding Cars data:" + e.getLocalizedMessage());
		}
		return 1;
	}
	
	public int removeCar() {
		try {
			sqlMapClient.insert("cars.removeCar", (new Car("Hundai", "Korea")));
		} catch (SQLException e) {
			log.error("Exception while removing Cars data:" + e.getLocalizedMessage());
		}
		return 1;
	}
	
	public int updateCar() {
		try {
			Map updateMap = new HashMap();
			updateMap.put("name", "Rocsts");
			updateMap.put("country", "Australia");
			
			return sqlMapClient.update("cars.updateCar", updateMap);
		} catch (SQLException e) {
			log.error("Exception while updating Cars data:" + e.getLocalizedMessage());
		}
		return 1;
	}
}
